﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Easily
{
    public partial class frmUartRecv : Form
    {
        private GeneralIcon MyIcon = null;  //保存传入的控件对象
        string recvLength = "";
        /// <summary>
        ///构造函数
        ///主要功能：加载保存的控件设置；生成初始代码；
        /// </summary>
        public frmUartRecv(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            //添加头文件
            this.MyIcon.code.HeadFile = "#include \"uart.h\"";
            //默认选中列表的第1个数据
            if (cBox_uart.Items.Count > 0)
                cBox_uart.SelectedIndex = 0;
            if (cBox_baud.Items.Count > 0)
                cBox_baud.SelectedIndex = 0;
            if (cbox_endSignal.Items.Count > 0)
                cbox_endSignal.SelectedIndex = 0;            
            //载入保存的参数:
            if (MyIcon.code.Save != "")
            {
                string[] tmp = MyIcon.code.Save.Split(';');
                txt_recvNum.Text = tmp[0];
                try
                {
                    recvLength = (Convert.ToInt32(txt_recvNum.Text) + 1).ToString();
                }
                catch
                {
                    MessageBox.Show("输入的长度有问题", "友情提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                lbl_gVar.Text = tmp[1];
                cBox_uart.SelectedItem = tmp[2];
                lbl_uart.Text = tmp[2];
                cBox_baud.SelectedItem = tmp[3];
                if (tmp[4] == "0") rbtn_recvCount.Checked = true;
                else if (tmp[4] == "1") rbtn_string.Checked = true;
                cbox_endSignal.SelectedItem = tmp[5];
            }
            btn_confirm_Click(new object(), new EventArgs());//生成默认代码
        }

        private void cBox_uart_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                recvLength = (Convert.ToInt32(txt_recvNum.Text) + 1).ToString();
            }
            catch
            {
                MessageBox.Show("输入的长度有问题", "友情提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            lbl_gVar.Text = "g" + recvLength + cBox_uart.SelectedItem.ToString() + "_Recv";
            lbl_uart.Text = cBox_uart.SelectedItem.ToString();
        }
        private void txt_recvNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txt_recvNum.Text != "")
                    recvLength = (Convert.ToInt32(txt_recvNum.Text) + 1).ToString();
            }
            catch
            {
                MessageBox.Show("输入的长度有问题", "友情提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            lbl_gVar.Text = "g" + recvLength + cBox_uart.SelectedItem.ToString() + "_Recv";
        }
        private void btn_confirm_Click(object sender, EventArgs e)
        {
            MyIcon.code.Clear();//清空以前代码
            string uart = cBox_uart.SelectedItem.ToString();
            MyIcon.code.Comment = "接收到串口" + uart + "的数据";
            MyIcon.code.Save = "";
            MyIcon.code.Initialization = "uart_init(" + uart + "," 
                + cBox_baud.SelectedItem.ToString() + ");//初始化串口";
            MyIcon.code.InterruptEnable = "uart_enable_re_int(" + uart + ");//开启串口中断";
            MyIcon.code.InterruptServiceStart = 
                //[2021/3/31]
                //"void " + uart.Replace("_", "") + "_IRQHandler(void)\r\n{\r\n"                + 
                "\tstatic uint8_t "  + "RecvCount = 0;//记录接收到的数据字节数\r\n"
                + "\tuint8_t ch;\r\n"
                + "\tuint8_t flag;\r\n"
                + "\tDISABLE_INTERRUPTS;          //关总中断\r\n"
                + "\tch = uart_re1(" + uart + ", &flag);//接收一个字节并清中断\r\n"
                + "\tif(flag)   //有数据\r\n"
                + "\t{\r\n ";
            MyIcon.code.InterruptServiceEnd = "";
            if (rbtn_recvCount.Checked)
            {
                //MyIcon.code.GlobalVariable = "\tuint8_t " + "guartrecv" + "[" + recvLength + "];";
                //MyIcon.code.InterruptServiceStart += "\t\t" + "guartrecv" + "[" + "RecvCount++] = ch;\r\n"
                //    + "\t\tif(" + "RecvCount == " + txt_recvNum.Text + ")\r\n\t\t{\r\n";
                //MyIcon.code.InterruptServiceEnd = "\t\t\t" + "RecvCount = 0;\r\n\t\t}\r\n";
            }
            else if(rbtn_string.Checked)
            {
                string signal = cbox_endSignal.SelectedItem.ToString();
                if (signal == "换行") signal = "\\n";

                MyIcon.code.GlobalVariable = "uint8_t g" + "recv[100]";
                MyIcon.code.InterruptServiceStart += "\t\tg"  + "recv" + "["  + "RecvCount++] = ch;\r\n"
                    + "\t\tif(ch == '" + signal + "')\r\n\t\t{\r\n\t\t\t" + "RecvCount = 0;\r\n";
                MyIcon.code.InterruptServiceEnd = "\t\t\tif("  + "RecvCount == 100)"  + "RecvCount = 0;\r\n\t\t}\r\n";
            }
            //uart_send1(" + uart + ", ch);//发送一个字节\r\n
            MyIcon.code.InterruptServiceEnd += "\t}\r\n";
            //+"\tENABLE_INTERRUPTS;       //开总中断\r\n}\r\n";
            string save4 = "";
            if (rbtn_recvCount.Checked) save4 = "0";
            else if (rbtn_string.Checked) save4 = "1";

            MyIcon.code.Save = txt_recvNum.Text + ";" + lbl_gVar.Text + ";"
                + cBox_uart.SelectedItem.ToString() + ";" + cBox_baud.SelectedItem.ToString()
                + ";" + save4 + ";" + cbox_endSignal.SelectedItem.ToString();
            this.DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// 单击“取消”按钮
        /// 主要功能：设置返回结果为取消
        /// </summary>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        /// <summary>
        /// 点击“注释说明”按钮
        /// 功能概要：为控件添加自定义注释说明
        /// </summary>
        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                    this.MyIcon.code.Comment = NE.NoteEdit;
            }
        }

        private void lbl_gVar_Click(object sender, EventArgs e)
        {

        }
    }
}
